package io.renren.modules.meta.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.meta.entity.EventEntity;
import io.renren.modules.meta.service.EventEntityService;
import io.renren.modules.meta.service.SubjectService;
import io.renren.modules.subject.dao.*;
import io.renren.modules.subject.entity.BaseSubjectTableInfo;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author linyuan
 * @email linyuan@gmail.com
 * @date 2023-08-25 18:40:01
 */
@RestController
@RequestMapping("meta/event")
public class EventController {
    @Autowired
    private EventEntityService eventService;
    @Resource
    private CarDao carDao;
    @Resource
    private PhoneDao phoneDao;

    @Resource
    private FraudulentPersonsDao fraudulentPersonsDao;

    @Resource
    private CriminalStrikePersonsDao criminalStrikePersonsDao;

    @Resource
    private PenaltyPersonsDao penaltyPersonsDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("meta:event:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = eventService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{eventId}")
    @RequiresPermissions("meta:event:info")
    public R info(@PathVariable("eventId") Long eventId) {
        EventEntity event = eventService.getById(eventId);
        List<BaseSubjectTableInfo> subjectTableInfoByEventId = eventService.getSubjectTableInfoByEventId(eventId);
        JSONObject result = new JSONObject();
        result.put("event", event);
        result.put("subjectList", subjectTableInfoByEventId);
        return R.ok().put("result", result.toJSONString());
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("meta:event:save")
    public R save(@RequestBody EventEntity event) {
        eventService.save(event);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("meta:event:update")
    public R update(@RequestBody EventEntity event) {
        eventService.updateById(event);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("meta:event:delete")
    public R delete(@RequestBody Long[] eventIds) {
        eventService.removeByIds(Arrays.asList(eventIds));

        return R.ok();
    }

    @PostMapping("/events")
    public R handleEvent(@RequestBody EventData eventData) {
        // 在这里处理事件数据
        String eventName = eventData.getEventName();
        LocalDateTime eventTime = eventData.getEventTime();
        String createdBy = eventData.getCreatedBy();
        String description = eventData.getDescription();
        EventEntity eventEntity = new EventEntity();
        eventEntity.setEventTime(eventTime);
        eventEntity.setEventName(eventName);
        eventEntity.setCreatedBy(createdBy);
        eventEntity.setDescription(description);
        eventService.save(eventEntity);
        List<EventData.SubjectData> subjectList = eventData.getSubjectList();
        for (EventData.SubjectData subjectData : subjectList) {
            switch (subjectData.subjectTableId) {
                case 1:
                    penaltyPersonsDao.insertSubjectEvent(subjectData.subjectId, eventEntity.getId(), "tb_penalty_persons_event");
                    break;
                case 2:
                    phoneDao.insertSubjectEvent(subjectData.subjectId, eventEntity.getId(), "tb_phone_event");
                    break;
                case 4:
                    fraudulentPersonsDao.insertSubjectEvent(subjectData.subjectId, eventEntity.getId(), "tb_fraudulent_persons_event");
                    break;
                case 5:
                    criminalStrikePersonsDao.insertSubjectEvent(subjectData.subjectId, eventEntity.getId(), "tb_criminal_strike_persons_event");
                    break;
                case 6:
                    carDao.insertSubjectEvent(subjectData.subjectId, eventEntity.getId(), "tb_car_event");
                    break;
                default:
                    return R.error("数据错误");
            }
        }
        return R.ok("事件已处理");
    }

    @PostMapping("/handleData")
    public R handleData(@RequestBody Event eventData) {
        // 根据mode属性来判断是用户属性还是用户行为，进行相应的处理逻辑
        if (eventData.getMode() == 1) {
            // 处理用户属性
            // ...
        } else if (eventData.getMode() == 2) {
            // 处理用户行为
            // ...
        }

        // 处理完成后，封装响应数据并返回给客户端
        return R.ok();
    }

    @lombok.Data
    public static class Event {
        private int id;
        private String eventName;
        private String propertyName;
        private String propertyValue;
        private String propertyDataType;
        private int mode;
        private String remark;

        // 省略getter和setter方法
    }

    @lombok.Data
    public static class EventData {
        private long t;
        private String eventName;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        private LocalDateTime eventTime;
        private String createdBy;
        private String description;
        private List<SubjectData> subjectList;

        // 省略getter和setter
        @lombok.Data
        public static class SubjectData {
            private Long subjectId;
            private Integer subjectTableId;
            private String subjectTable;

            // 省略getter和setter
        }
    }

    @lombok.Data
    @AllArgsConstructor
    public static class Data {
        private Long subjectId;
        private Long subjectTableId;
        private String subjectTable;
    }
}
